package com.totoro.tygeneratedragonblock.manager;

import com.totoro.tygeneratedragonblock.TyGenerateDragonBlock;
import com.totoro.tygeneratedragonblock.pojo.Config;
import com.totoro.tygeneratedragonblock.utils.ColorUtil;
import lombok.Getter;
import org.bukkit.configuration.file.FileConfiguration;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Totoro
 * @version 0.1.0
 * @date 2025/8/10 - 2:33
 * @description
 **/
public class ConfigManager {

    private final TyGenerateDragonBlock plugin;

    @Getter
    private final Config config;

    public ConfigManager(TyGenerateDragonBlock plugin) {
        this.plugin = plugin;
        config = new Config();
        initConfig();
        reloadConfig();
    }

    private void initConfig() {
        if (!this.plugin.getDataFolder().exists()){
            this.plugin.getDataFolder().mkdirs();
            this.plugin.saveDefaultConfig();
        }
    }

    public void reloadConfig() {
        plugin.reloadConfig();
        FileConfiguration configuration = plugin.getConfig();
        boolean debug = configuration.getBoolean("debug", false);
        String prefix = ColorUtil.color(configuration.getString("prefix", "&6[TyGenerateDragonBlock] "));
        List<String> itemMatch = configuration.getStringList("item-match").stream().map(ColorUtil::color).collect(Collectors.toList());
        Boolean enableBlockNameDisplay = configuration.getBoolean("enable-block-name-display", false);
        config.setDebug(debug);
        config.setPrefix(prefix);
        config.setItemMatch(itemMatch);
        config.setEnableBlockNameDisplay(enableBlockNameDisplay);
    }
}
